package com.baidu.bas.upms.model;

import com.baidu.bas.annotation.Erupt;
import com.baidu.bas.annotation.EruptField;
import com.baidu.bas.annotation.sub_erupt.Drill;
import com.baidu.bas.annotation.sub_erupt.Link;
import com.baidu.bas.annotation.sub_erupt.Power;
import com.baidu.bas.annotation.sub_field.Edit;
import com.baidu.bas.annotation.sub_field.View;
import com.baidu.bas.annotation.sub_field.sub_edit.Search;
import com.baidu.bas.upms.model.base.HyperModel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import java.util.List;

/**
 * @author liyuepeng
 * @date 2018-12-07.
 */

@Entity
@Table(name = "zero_dict", uniqueConstraints = @UniqueConstraint(columnNames = "code"))
@Erupt(
        name = "数据字典",
        power = @Power(export = true),
        drills = @Drill(
                code = "item",
                title = "字典项",
                link = @Link(
                        linkErupt = EruptDictItem.class, joinColumn = "eruptDict.id"
                )
        )
)
@Getter
@Setter
@NoArgsConstructor
public class EruptDict extends HyperModel {

    @EruptField(
            views = @View(title = "编码", sortable = true),
            edit = @Edit(title = "编码", notNull = true, search = @Search(vague = true))
    )
    private String code;

    @EruptField(
            views = @View(title = "名称", sortable = true),
            edit = @Edit(title = "名称", notNull = true, search = @Search(vague = true))
    )
    private String name;

    public EruptDict(String code, String name) {
        this.code = code;
        this.name = name;
    }

    public EruptDict(String code) {
        this.code = code;
        this.name = code;
    }

    @EruptField(
            views = @View(title = "备注"),
            edit = @Edit(
                    title = "备注"
            )
    )
    private String remark;

    /**
     * 设置dict与item之间的关系
     * @param itemList
     */
    public void initDictItemRel(List<EruptDictItem> itemList){
        itemList.forEach(it->{
            it.setEruptDict(this);
        });
    }

}
